//package ai.people.core.web.ascpet;
//
//import ai.people.core.web.annotation.AuthCheck;
//import ai.people.netmon.framework.domain.ucenter.ext.Authorities;
//import ai.people.netmon.framework.domain.ucenter.request.LoginUser;
//import ai.people.netmon.framework.exception.enums.AuthExceptionEnum;
//import ai.people.netmon.framework.exception.type.AuthException;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.CollectionUtils;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Method;
//import java.util.Arrays;
//import java.util.List;
//import java.util.Set;
//import java.util.stream.Stream;
//
//import static java.util.stream.Collectors.toSet;
//
///**
// * @author zhenglin
// * @apiNote 检查角色注解实现类
// * @since 2019/9/16 14:39
// */
//@Aspect
//@Component
//@Slf4j
//public class AuthCheckAspect {
//
//    @Before("execution(* *..controller..*(..))")
//    public void before(JoinPoint joinPoint) {
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        AuthCheck annotation = method.getAnnotation(AuthCheck.class);
//
//        if (annotation == null) {
//            //获取类上注解
//            annotation = joinPoint.getTarget().getClass().getAnnotation(AuthCheck.class);
//        }
//
//        if (annotation != null) {
//            LoginUser loginUser = RequestHolderUtil.loginUser();
//            boolean roleCheck = true;
//            boolean permissionCheck = true;
//
//            AuthCheck.Operator operator = annotation.operator();
//
//            String[] checkRoles = annotation.roles();
//            if (checkRoles.length != 0) {
//                List<Long> userRoleIds = loginUser.getUserRoleId();
//                roleCheck = isCheck(checkRoles, annotation.rolesOperator(), CollectionUtils.isEmpty(userRoleIds), userRoleIds.stream().map(String::valueOf));
//            }
//
//            String[] permissions = annotation.permissions();
//            if (permissions.length != 0) {
//                List<Authorities> authorities = loginUser.getAuthorities();
//                permissionCheck = isCheck(permissions, annotation.permissionsOperator(), CollectionUtils.isEmpty(authorities), authorities.stream().map(Authorities::getAuthority));
//            }
//
//            if (operator == AuthCheck.Operator.AND) {
//                if (!(roleCheck && permissionCheck)) {
//                    throw new AuthException(AuthExceptionEnum.INSUFFICIENT_PERMISSIONS);
//                }
//            }else {
//                if (!(roleCheck || permissionCheck)) {
//                    throw new AuthException(AuthExceptionEnum.INSUFFICIENT_PERMISSIONS);
//                }
//            }
//        }
//
//    }
//
//    private boolean isCheck(String[] checkArr, AuthCheck.Operator opt, boolean empty, Stream<String> stringStream) {
//        boolean flag = opt == AuthCheck.Operator.AND;
//        if (empty) {
//            return false;
//        } else {
//            Set<String> has = stringStream.collect(toSet());
//            return check(checkArr, has, flag);
//        }
//    }
//
//    private boolean check(String[] roles, Set<String> hasRoles, boolean opt) {
//        if (CollectionUtils.isEmpty(hasRoles)) {
//            return false;
//        }
//        List<String> roleList = Arrays.asList(roles);
//        if (opt) {
//            return hasRoles.containsAll(roleList);
//        } else {
//            for (String hasRole : hasRoles) {
//                if (roleList.contains(hasRole)) {
//                    return true;
//                }
//            }
//            return false;
//        }
//    }
//
//}
